package com.lxy.config;

import com.lxy.common.JacksonObjectMapper;
import com.lxy.interceptor.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;
    @Autowired
    private JwtTokenInterceptorBusiness jwtTokenInterceptorBusiness;
    @Autowired
    private JwtTokenInterceptorShop jwtTokenInterceptorShop;
    @Autowired
    private JwtTokenInterceptorUser jwtTokenInterceptorUser;
    @Autowired
    private JwtTokenInterceptorRider jwtTokenInterceptorRider;




    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
    }


    /**
     * 通过knife4j生成接口文档
     * @return
     */
//    @Bean
//    public Docket docket1() {
//        log.info("准备生成接口文档1...");
//        ApiInfo apiInfo = new ApiInfoBuilder()
//                .title("跑腿接单接口1")
//                .version("2.0")
//                .description("跑腿接单接口文档1")
//                .build();
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .groupName("管理员接口文档")
//                .apiInfo(apiInfo)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.lxy.controller.Admin"))
//                .paths(PathSelectors.any())
//                .build();
//        return docket;
//    }
//
//    @Bean
//    public Docket docket2() {
//        log.info("准备生成接口文档2...");
//        ApiInfo apiInfo = new ApiInfoBuilder()
//                .title("跑腿接单接口2")
//                .version("2.0")
//                .description("跑腿接单接口文档2")
//                .build();
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .groupName("商家端接口文档")
//                .apiInfo(apiInfo)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.lxy.controller.BusinessMan"))
//                .paths(PathSelectors.any())
//                .build();
//        return docket;
//    }

    @Bean
    public Docket docket() {
        log.info("准备生成接口文档...");
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("跑腿接单接口")
                .version("2.0")
                .description("跑腿接单接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("接口文档")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lxy.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }


    /**
     * 注册自定义拦截器
     *
     * @param registry
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/registration")
                .excludePathPatterns("/admin/sendCode")
                .excludePathPatterns("/admin/forget");
        registry.addInterceptor(jwtTokenInterceptorBusiness)
                .addPathPatterns("/business/**")
                .excludePathPatterns("/business/login")
                .excludePathPatterns("/business/sendCode")
                .excludePathPatterns("/business/registration")
                .excludePathPatterns("/business/forget");
        registry.addInterceptor(jwtTokenInterceptorShop)
                .addPathPatterns("/shop/**")
                .excludePathPatterns("/shop/login")
                .excludePathPatterns("/shop/registration");
        registry.addInterceptor(jwtTokenInterceptorUser)
                .addPathPatterns("/user/**")
                .addPathPatterns("/addressBook/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/registration");
        registry.addInterceptor(jwtTokenInterceptorRider)
                .addPathPatterns("/rider/**")
                .excludePathPatterns("/rider/login")
                .excludePathPatterns("/rider/registration")
                .excludePathPatterns("/rider/forget");

    }


    /**
     * 扩展mvc框架的消息转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("消息拓展其启动成功...");
        MappingJackson2HttpMessageConverter messageConverter=new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0,messageConverter);
    }
}
